package com.iwdnb.gkgz.application.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import com.iwdnb.bmnf.common.utils.BooleanUtils;
import com.iwdnb.gkgz.application.service.StockLimitUpService;
import com.iwdnb.gkgz.common.model.dto.StockDayData;
import com.iwdnb.gkgz.common.model.dto.StockLimitUpData;
import com.iwdnb.gkgz.common.utils.BigDecimalUtils;
import com.iwdnb.gkgz.common.utils.StockUtils;
import com.iwdnb.gkgz.common.utils.WorkDayUtils;
import com.iwdnb.gkgz.infrastructure.dao.model.query.StockLimitUpQuery;
import com.iwdnb.gkgz.infrastructure.dao.repo.StockDayDataRepository;
import com.iwdnb.gkgz.infrastructure.dao.repo.StockLimitUpRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class StockLimitUpServiceImpl implements StockLimitUpService {
    @Autowired
    private StockLimitUpRepository stockLimitUpRepository;
    @Autowired
    private StockDayDataRepository stockDayDataRepository;

    @Override
    public List<StockLimitUpData> queryLimitUpList(StockLimitUpQuery query) {
        return stockLimitUpRepository.queryLimitUpList(query);
    }

    @Override
    public void setTldFlag(Date date) {
        Date beforeDateBegin = DateUtil.beginOfDay(date);
        Date beforeDateEnd = DateUtil.endOfDay(new Date());
        StockLimitUpQuery query = new StockLimitUpQuery();
        query.setDateBegin(DateUtil.formatDateTime(beforeDateBegin));
        query.setDateEnd(DateUtil.formatDateTime(beforeDateEnd));
        query.setTldFlag(BooleanUtils.FALSE);
        List<StockLimitUpData> limitUpDataList = queryLimitUpList(query);
        if (CollectionUtils.isEmpty(limitUpDataList)) {
            return;
        }
        for (StockLimitUpData data : limitUpDataList) {
            String dateStr = DateUtil.formatDate(data.getDate());
            BigDecimal price = data.getClosePrice();
            List<StockDayData> stockDayDataList = stockDayDataRepository.queryAll(data.getCode());
            List<StockDayData> beforeDatas = StockUtils.getStockDayDataListBeforeDate(stockDayDataList, dateStr, 1);
            if (CollectionUtils.isNotEmpty(beforeDatas) && StockUtils.isLimitUp(beforeDatas.get(0))) {
                data.setTldFlag("d");
                stockLimitUpRepository.saveStockLimitUpData(data);
                continue;
            }
            List<StockDayData> afterDatas = StockUtils.getStockDayDataListAfterDate(stockDayDataList, dateStr, 1);
            if (CollectionUtils.isNotEmpty(afterDatas)) {
                StockDayData afterData = afterDatas.get(0);
                if (StockUtils.isLimitUp(afterData)) {
                    data.setTldFlag("d");
                    stockLimitUpRepository.saveStockLimitUpData(data);
                    continue;
                }
                if (BigDecimalUtils.isGe(afterData.getRate(), new BigDecimal(8))) {
                    data.setTldFlag("d");
                    stockLimitUpRepository.saveStockLimitUpData(data);
                    continue;
                }
                BigDecimal rate = BigDecimalUtils.subStractAndDividePrecent(afterData.getClosePrice(), price);
                if (BigDecimalUtils.isGe(rate, new BigDecimal(10))) {
                    data.setTldFlag("d");
                    stockLimitUpRepository.saveStockLimitUpData(data);
                    continue;
                }
            }
            //两天后还在10个点以上的，不计入屠龙刀
            afterDatas = StockUtils.getStockDayDataListAfterDate(stockDayDataList, dateStr, 2);
            if (CollectionUtils.isNotEmpty(afterDatas) && afterDatas.size() == 2) {
                StockDayData afterData = afterDatas.get(1);
                BigDecimal rate = BigDecimalUtils.subStractAndDividePrecent(afterData.getClosePrice(), price);
                if (BigDecimalUtils.isGe(rate, new BigDecimal(10))) {
                    data.setTldFlag("d");
                    stockLimitUpRepository.saveStockLimitUpData(data);
                    continue;
                }
            }
        }
    }
}
